package collection

/**
 * 集合
 */
fun main() {

    //数组
    val array: Array<Int> = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9)

    println("array.size=${array.size}  array.first()=${array.first()}")

    for ((index, element) in array.withIndex()) {
        println("index=${index} element=${element}")
    }

    val array1: Array<Int> = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
    println("array == array1:${array == array1}  equals=${array.equals(array1)}")

    val copyOfRange = array.copyOfRange(0, 3);
    println("array.copyOfRange(0, 3)= ${copyOfRange.size}")

    //原生类型数组
    val intArrayOf = intArrayOf(1, 2, 4, 5, 6)
    val longArrayOf = longArrayOf(1, 2, 3, 4, 5, 6)

    //嵌套数组
    val arrayOf = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6), intArrayOf(7, 8, 9))
    for (ints in arrayOf) {
        for (i in ints) {
            print("$i,")
        }
        println("")
    }


    //List   有序集合 有索引 可以包含重复元素
    val mList: MutableList<Int> = mutableListOf<Int>(1, 2, 3, 4, 5, 6) // 可变数组
    mList.add(12)
    mList.removeAt(0)
    mList[0]
    mList[0] = 12

    val list: List<Int> = listOf<Int>(1, 2, 3, 4, 5, 6) //不可变的
    val listOfNotNull = listOfNotNull(1, 2, 4, 5, null)//过滤null数据

    //set   无序集合 不可重复
    val mSet: MutableSet<Int> = mutableSetOf(1, 2, 2, 3, 4, 4, 5)
    val mSet2: MutableSet<Int> = mutableSetOf(1, 2, 2, 3, 4, 4, 5)
    mSet.add(6)
    mSet.size
    mSet.remove(1)

    mSet union mSet2  //求两个集合的并集
    mSet intersect mSet2  //求两个集合的交集
    mSet subtract mSet2  //求两个集合的差集

    //Map   键值对
    val mMap = mutableMapOf<Int, String>(1 to "AAA", 2 to "BBB", 3 to "CCC", 4 to "DDD")
    val keys = mMap.keys
    val values = mMap.values
    mMap[5] = "EEE"
    mMap.put(6, "FFF")
    println("mMap[1]= ${mMap[1]}  ")

    //集合操作
    val toMutableList = array.toMutableList()
    val toMutableSet = array.toMutableSet();

    val toMutableList1 = array.map { it.toString() }.toMutableList()

    val list1: List<Int> = listOf<Int>(1, 2, 3)
    val list2: List<String> = listOf<String>("a", "b", "c")
    val zip: List<Pair<Int, String>> = list1.zip(list2)
    val unzip: Pair<List<Int>, List<String>> = zip.unzip();
    unzip.first
    unzip.second

    //序列
    val sequence: Sequence<Int> = generateSequence { 10 }

}